<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function generate_css() {
    global $config;

    $res = '';
    foreach ($config['css']as $v) {
        $res .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"$v\"/>";
    }
    return $res;
}

function call_block($key) {
    global $config;
    @list($c, $f, $p) = @explode(".", $key, 3);
    if (file_exists($config['base_dir'] . "/block/$c.php"))
        require_once ($config['base_dir'] . "/block/$c.php");
    else
        return "";
    $cc = single($c);

    $params = array();
    if (isset($p))
        $params = explode('.', $p);

    if ($f == null)
        $f = 'index';
    return @call_user_func_array(array($cc, $f), $params);
}

function core($corename) {
    global $config;
    $corename .= "Core";
    require_once ($config['base_dir'] . "/core/$corename.php");
    return single($corename);
}



function generate_js() {
    global $config;

    $res = '';
    foreach ($config['js']as $v) {
        $res .= "<script src=\"$v\" type=\"text/javascript\"></script>";
    }
    return $res;
}

function document($key, $view = null) {
    global $config;
    if (isset($config['document'][$key]))
        return $config['document'][$key];
    else {
        $data = call_block($key);
        register($key, $data, $view);
        return $config['document'][$key];
    }
}

function register($key, $data, $view) {
    global $config;

    if ($view) {
        $view_file = $config['base_dir'] . "/theme/" . $config['theme'] . "/view/$view" . ".php";
        if (!file_exists($view_file))
            $view_file = $config['base_dir'] . "/view/$view" . ".php";


        if (file_exists($view_file)) {
            ob_start();
            require("$view_file");
            $value = ob_get_clean();
            $config['document'][$key] = $value;
        } else {
            $config['document'][$key] = $data;
        }        
    } else {
        $config['document'][$key] = $data;
    }
}

function content($data, $view = 'content') {
    register("content", $data, $view);
}



?>
